package com.handong.handserialprogram.module.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.module.createmission.CreateMissionActivity;

/**
 * Created by userKey on 2017/8/7.
 */

public abstract class SimpleFragment extends BackStackFragment {

    protected CreateMissionActivity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof CreateMissionActivity){
            mActivity = (CreateMissionActivity) context;
        }
    }

    public void addFragment(BackStackFragment fragment) {
        addFrag(fragment, R.id.createMissionContainer);
    }

    public void removeFragment() {
        removeFrag(null);
    }

    public void showLoadDialog(String content) {
        BaseActivity activity = (BaseActivity) getActivity();
        activity.showLoadDialog(content);
    }

    public void dismissLoadingDialog() {
        BaseActivity activity = (BaseActivity) getActivity();
        activity.dismissLoadingDialog();
    }

    protected LinearLayout showTipLayout(int imageId, int textId, View.OnClickListener listener) {
        LinearLayout layout = (LinearLayout) LayoutInflater.from(getContext().getApplicationContext())
                .inflate(R.layout.load_result_tip, null);
        ImageView imageView = (ImageView) layout.getChildAt(0);
        imageView.setImageResource(imageId);
        TextView tipText = (TextView) layout.getChildAt(1);
        tipText.setText(textId);

        if (null != listener){
            layout.setOnClickListener(listener);
        }
        return layout;
    }

    protected void openAct(Class<? extends BaseActivity> cls, Bundle bundle) {
        BaseActivity activity = (BaseActivity) getActivity();
        Intent intent = new Intent(activity, cls);
        intent.putExtras(bundle);
        activity.startActivity(intent);
        activity.finish();
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.anim_fade_out);
    }

    @Override
    public void onDetach() {
        mActivity = null;
        super.onDetach();
    }
}
